package com.ruoyi.parkinglot.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.parkinglot.domain.ParkSpot;
import com.ruoyi.parkinglot.domain.vo.ParkingStatusVO;
import com.ruoyi.parkinglot.mapper.ParkSpotMapper;
import com.ruoyi.parkinglot.service.ParkSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import static com.ruoyi.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("/ParkSpot")
public class ParkSpotController extends BaseController {
    @Resource
    private ParkSpotService parkSpotService;


    /**
     * 获取停车场现状列表
     */
    @GetMapping("/status")
    public TableDataInfo getParkingStatus() {
        startPage();
        List<ParkingStatusVO> list = parkSpotService.selectParkingStatus();
        return getDataTable(list);
    }

    /**
     * 获取泊位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ParkSpot parkSpot) {
        startPage();
        List<ParkSpot> list = parkSpotService.list();
        return getDataTable(list);
    }

    /**
     * 获取泊位详细信息
     */
    @GetMapping("/{spotId}")
    public AjaxResult getInfo(@PathVariable Long spotId) {
        return success(parkSpotService.getById(spotId));
    }

    /**
     * 新增泊位
     */
    @PostMapping
    public AjaxResult add(@RequestBody ParkSpot parkSpot) {
        boolean success = parkSpotService.save(parkSpot);
        return success ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改泊位
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ParkSpot parkSpot) {
        boolean success = parkSpotService.updateById(parkSpot);
        return success ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除泊位
     */
    @DeleteMapping("/{spotIds}")
    public AjaxResult remove(@PathVariable Long[] spotIds) {
        boolean success = parkSpotService.removeByIds(Arrays.asList(spotIds));
        return success ? AjaxResult.success() : AjaxResult.error();
    }
}
